package com.demo.controller;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.entity.UserTest;
import com.demo.service.UserTestService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * (UserTest)表控制层
 *
 * @author codor
 * @since 2023-10-09 16:25:32
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/userTest")
public class UserTestController {

    /**
     * 服务对象
     */
    private final UserTestService userTestService;

    /**
     * 新增修改数据(数据库中存在id修改, 不存在新增)
     *
     * @return 操作结果
     */
    @RequestMapping("/createUpdate")
    public boolean createUpdate() {
        return this.userTestService.saveOrUpdate(getOne());
    }

    /**
     * 批量保存
     *
     * @return 操作结果
     */
    @RequestMapping("/createBatch")
    public boolean createBatch() {
        List<UserTest> list = new ArrayList<>();
        list.add(getOne());
        list.add(getOne());
        list.add(getOne());
        list.add(getOne());
        list.add(getOne());
        return this.userTestService.saveBatch(list);
    }

    /**
     * 批量更新
     *
     * @return 操作结果
     */
    @RequestMapping("/updateBatch")
    public boolean updateBatch() {
        List<UserTest> list = new ArrayList<>();
        list.add(getOne(1));
        list.add(getOne(2));
        list.add(getOne(3));
        list.add(getOne(4));
        list.add(getOne(5));
        return this.userTestService.updateBatchById(list);
    }

    /**
     * 通过主键查询单条数据
     *
     * @return 单条数据
     */
    @RequestMapping("/detail")
    public UserTest detail() {
        return this.userTestService.getById(1);
    }

    /**
     * 分页查询所有数据
     *
     * @return 分页结果
     */
    @RequestMapping("/page")
    public List<UserTest> page() {
        return this.userTestService
                .lambdaQuery()
                .like(UserTest::getNum, "num")
                .like(UserTest::getName, "b")
                .orderByAsc(UserTest::getId)
                .page(Page.of(1, 2)).getRecords();
    }

    /**
     * 获取所有数据
     *
     * @return 列表结果
     */
    @RequestMapping("/list")
    public List<UserTest> list() {
        return this.userTestService.lambdaQuery().orderByAsc(UserTest::getId).list();
    }

    /**
     * 删除数据
     *
     * @return 删除结果
     */
    @RequestMapping("/delete")
    public boolean delete() {
        return this.userTestService
                .lambdaUpdate()
                .eq(UserTest::getId, 2)
                .set(UserTest::getUpdateTime, LocalDateTime.now())
                .set(UserTest::getDeleteTime, LocalDateTime.now())
                .update();
    }

    // ----------------------------------------------------------------------------------- private method

    private UserTest getOne() {
        return new UserTest("num" + RandomUtil.randomString(3),
                "name" + RandomUtil.randomString(4),
                "note" + RandomUtil.randomString(5));
    }

    private UserTest getOne(Integer id) {
        UserTest userTest = new UserTest("num" + RandomUtil.randomString(3),
                "name" + RandomUtil.randomString(4),
                "note" + RandomUtil.randomString(5));
        userTest.setId(id);
        return userTest;
    }
}

